import check_utils from "../../components/custom_views/check_utils"
import { Dict } from "../../components/custom_views/dict"
import {
  checkBase, checkLicense, checkPic, checkSell, checkVideo,
  finishBase, finishFeature, finishLicense, finishPic, finishVideo, checkFeature
} from "./post_add_sell_fake_dto"
import { getDefaultPostDto } from "./post_add_sell_fake_dto"
import { Api } from './../../api/api'
import { CustomDict } from "../../components/custom_views/custom_dict"
import { CustomTips } from "../../components/custom_views/custom_tips"
export const post_add_fake_mixin = {

  initData: (that) => {

    if (that && that.$store.state.myPostOfEditingSell && that.$store.state.myPostOfEditingSell.sourceId) {
      return {
        postDto: that.$store.state.myPostOfEditingSell
      }
    } else {
      let postDtoJson = uni.getStorageSync('post_sell_draft')
      if (postDtoJson) {
        return {
          postDto: JSON.parse(postDtoJson)
        }
      } else {
        return {
          postDto: getDefaultPostDto(Dict.PostMainTypeEnum.Shop.childs.ShopSell.value)
        }
      }
    }
  },
  methods: {
    finished(part) {
      if (part == 'base') {
        return finishBase(this.postDto)
      } else if (part == 'pic') {
        return finishPic(this.postDto)
      } else if (part == 'license') {
        return finishLicense(this.postDto)
      } else if (part == 'video') {
        return finishVideo(this.postDto)
      } else if (part == 'feature') {
        return finishFeature(this.postDto)
      } else {
        return 0
      }
    },
    postAdd(part) {
      
      let that = this
      let result = false
      if (part == 'base') {
        result = checkBase(this.postDto)
      } else if (part == 'pic') {
        result = checkPic(this.postDto)
      } else if (part == 'license') {
        result = checkLicense(this.postDto)
      } else if (part == 'video') {
        result = checkVideo(this.postDto)
      } else if (part == 'feature') {
        result = checkFeature(this.postDto)
      } else {
        result = checkSell(this.postDto)
        if (result) {
          this.$Api.CustRequestSubscribeMessage(
            () => {
              this.$Api.PostSellApi.add(that.postDto, (res) => {
                if (res.code == 200) {
                  uni.removeStorageSync('post_sell_draft')
                  that.$store.commit('updateMyPostOfEditingSell', {})
                  this.showCustomTips({
                    title: '温馨提示',
                    content: CustomTips.tipsAfterAddSellPost,
                    confirmText: '确定',
                    confirmColor: '',
                    cancelText: '',
                    success(res){
                      if(res.confirm){
                        uni.redirectTo({
                          url: '/pages/home/home'
                        })
                      }
                    }
                  })
                }
              })
            },
            () => {
              this.$Api.PostSellApi.add(that.postDto, (res) => {
                if (res.code == 200) {
                  uni.removeStorageSync('post_sell_draft')
                  that.$store.commit('updateMyPostOfEditingSell', {})
                  this.showCustomTips({
                    title: '温馨提示',
                    content: CustomTips.tipsAfterAddSellPost,
                    confirmText: '确定',
                    confirmColor: '',
                    cancelText: '',
                    success(res){
                      if(res.confirm){
                        uni.redirectTo({
                          url: '/pages/home/home'
                        })
                      }
                    }
                  })
                }
              })
            }
          )

        }
        return
      }
      if (result) {

        if (this.$store.state.myPostOfEditingSell && this.$store.state.myPostOfEditingSell.sourceId) {
          this.$store.commit('updateMyPostOfEditingSell', this.postDto)
          uni.showToast({
            title: '保存成功',
            icon: "none",
            duration: 800,
            success() {
              setTimeout(() => {
                uni.navigateBack({
                  delta: 1
                })
              }, 900)
            },
          })
        } else {
          uni.setStorageSync('post_sell_draft', JSON.stringify(this.postDto))
          uni.showToast({
            title: '保存成功',
            icon: "none",
            duration: 800,
            success() {
              setTimeout(() => {
                uni.navigateBack({
                  delta: 1
                })
              }, 900)
            },
          })
        }

      } else {
        // this.$CheckUtils.showCustToast('保存失败')
      }
    },
  }
}